package com.epoint.mobileoa.personalized.epointcompany.ui;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetServerTimeTask;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.GetChuchaijihuaListTask;
import com.epoint.mobileoa.personalized.epointcompany.model.ChuchaijihualistModel;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.StringHelp;

/**
 * @author liyc
 * @time 2012-4-18 下午7:28:01
 * @annotation 出差计划列表
 */
public class ChuChaijihuaList extends SuperView {

	private ListView lv;

	private int pagecount = 30;//每页显示数量

	private int pageindex = 1;//第几页

	public static int allxmnums;//所有邮件数量


	private Button load_bt;//加载中的文本
	private LinearLayout foot;

	private int visibleItemCount;//一页可显示的List数量

	private String todaystr;

	//	private List<ChuchaijihualistModel> list = new ArrayList<ChuchaijihualistModel>();

	private int taskid;//1获取服务器时间

	private Button btXzrq,btXzccjh;

	@Override
	public void initControls() {
		btXzrq = (Button) findViewById(R.id.btXzrq);
		btXzrq.setOnClickListener(this);
		btXzccjh = (Button) findViewById(R.id.btXzccjh);
		btXzccjh.setOnClickListener(this);
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setText("新增");
		top_right_bt.setOnClickListener(this);
		lv = (ListView) findViewById(R.id.lvCcjh);
		lv.setOnItemClickListener(this);
		getServerTime();

		/*lv = (ListView) findViewById(R.id.lvCcjh);
		addFoot();


		getData(pageindex,pagecount);*/
	}


	public void getServerTime(){
		showDialog_wait(null);
		taskid = 1;
		Task task = new GetServerTimeTask(this, null);
		MainService.addTask(task);
	}

	public void getData(int pageindex, int pagecount){
		showDialog_wait(null);
		HashMap param=new HashMap();

		param.put("pageindex", pageindex);
		param.put("pagecount", pagecount);

		Task task = new GetChuchaijihuaListTask(this, param);
		MainService.addTask(task);
	}

	public void addFoot(){
		load_bt = new Button(this);
		load_bt.setBackgroundResource(R.drawable.button_login);

		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
		load_bt.setOnClickListener(this);
	}

	List<Date> dlist = new ArrayList<Date>();
	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskid==1){
				Date date = (Date) param[1];
				dlist.add(new Date(date.getTime()+4*24*60*60*1000));
				dlist.add(new Date(date.getTime()+3*24*60*60*1000));
				dlist.add(new Date(date.getTime()+2*24*60*60*1000));
				dlist.add(new Date(date.getTime()+1*24*60*60*1000));
				dlist.add(date);
				dlist.add(new Date(date.getTime()-1*24*60*60*1000));
				dlist.add(new Date(date.getTime()-2*24*60*60*1000));
				dlist.add(new Date(date.getTime()-3*24*60*60*1000));
				dlist.add(new Date(date.getTime()-4*24*60*60*1000));
				dlist.add(new Date(date.getTime()-5*24*60*60*1000));
				dlist.add(new Date(date.getTime()-6*24*60*60*1000));
				dlist.add(new Date(date.getTime()-7*24*60*60*1000));
				dlist.add(new Date(date.getTime()-8*24*60*60*1000));
				todaystr = DateHelp.convertDate(date, "yyyy-MM-dd");

				MyAdapter adapter = new MyAdapter();
				lv.setAdapter(adapter);
			}
		}
		m_Dialog.dismiss();
	}

	@Override
	public int getLayoutInt() {
		return R.layout.chuchaijihualist;
	}

	@Override
	public String setTitle() {
		return "出差计划";
	}

	@Override
	public void onClick(View v) {
		if(v==load_bt){
			pageindex++;
			getData(pageindex, pagecount);
		}
		else if(v==top_right_bt){
			if(todaystr!=null){
				Intent it = new Intent(this,AddNewTripPlanView.class);
				it.putExtra("writeDate", todaystr);
				startActivity(it);
			}
		}
		else if(v==btXzccjh){
			if(todaystr!=null){
				Intent it = new Intent(this,AddNewTripPlanView.class);
				it.putExtra("writeDate", todaystr);
				startActivity(it);
			}
		}
		else if(v==btXzrq){
			AlertHelp.showDatePickDialog(
					this, 
					new OnDateSetListener() {
						@Override
						public void onDateSet(DatePicker view, int year, int monthOfYear,
								int dayOfMonth) {
							String pickdate = year + "-" + DateHelp.switchDay((monthOfYear+1)) + "-" + DateHelp.switchDay(dayOfMonth);
							intent(pickdate);
						}
					},
					"选择查询日期");
		}
	}

	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return dlist.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(ChuChaijihuaList.this);
				convertView = mInflater.inflate(R.layout.chuchaijihualiststyle, null);
				holder.ml_mailimg = (ImageView) convertView
						.findViewById(R.id.ml_mailimg);
				holder.ml_attach = (ImageView) convertView
						.findViewById(R.id.ml_attach);
				holder.ml_mailtitle = (TextView) convertView
						.findViewById(R.id.ml_mailtitle);
				holder.ml_sandt = (TextView) convertView
						.findViewById(R.id.ml_sandt);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.ml_mailimg.setVisibility(View.GONE);

			String datestr = DateHelp.convertDate(dlist.get(position), "yyyy-MM-dd");


			Date dateLong = DateHelp.convertString2Date(datestr+" 00:00:00", "yyyy-MM-dd hh:mm:ss");

			String week = DateHelp.getWeekByDate(dateLong);

			String fontColor = "";
			if(getBackgroundStr().equals("background")){
				fontColor = "white";
			}
			else{
				fontColor = "black";
			}
			
			if(week.equals("星期日")){
				week = "<font color=#FF2D2D>(星期日)</font>";
			}
			else{
				week = "<font color='"+fontColor+"'>("+week+")</font>";
			}
			
			String displaystr = "<font color='"+fontColor+"'>"+datestr+"</font>"+week;
			if(datestr.equals(todaystr)){
				displaystr += "<font color='"+fontColor+"'>[今天]</font>";
			}

			holder.ml_mailtitle.setText(Html.fromHtml(displaystr));
			holder.ml_sandt.setVisibility(View.GONE);
			return convertView;
		}
	}

	public class ViewHolder {
		public ImageView ml_mailimg;
		public ImageView ml_attach;
		public TextView ml_mailtitle;
		public TextView ml_sandt;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		String datestr = DateHelp.convertDate(dlist.get(position), "yyyy-MM-dd");
		intent(datestr);
	}

	private void intent(String datestr){
		Intent it = new Intent(this,ChuchaijihuaDetailListView.class);
		it.putExtra("date", datestr);
		startActivity(it);
	}


	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}
}
